﻿/*
 XsltDb is powerful XSLT module for DotnetNuke.
 It offers safe database access, SEO-friendly AJAX support,
 visitor interactions, environment integration (dnn properties,
 request, cookie, session and form values), regular expressions, etc.

 Author:
 
    Anton Burtsev
    burtsev@yandex.ru

 Project home page: 
 
    http://xsltdb.codeplex.com
*/
using System;
using System.IO;
using System.Xml.XPath;
using System.Collections.Generic;
using System.Web;

using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.Localization;

namespace Findy.XsltDb
{

    public class XsltDbResX
    {
        private class ResGroup
        {
            Dictionary<string, string> values = new Dictionary<string, string>();
            public ResGroup(string path)
            {
                if (File.Exists(path))
                {
                    XPathDocument doc = new XPathDocument("file:///" + path);

                    foreach (XPathNavigator n in doc.CreateNavigator().Select("/root/data"))
                        values[n.SelectSingleNode("@name").Value] = n.SelectSingleNode("value").Value;
                }
            }

            public string GetString(string key)
            {
                if (values.ContainsKey(key))
                    return values[key];
                return key;
            }
            public bool Contains(string key)
            {
                return values.ContainsKey(key);
            }
        }

        private class RecurciveLanguageSearcher
        {
            Dictionary<string, bool> alreadySearched = new Dictionary<string, bool>();

            private string GetStringNoFallBack(string path, string language, int PortalID, string key)
            {
                ResGroup group;
                group = GetGroup(path, language, "Portal", PortalID);
                if (group.Contains(key))
                    return group.GetString(key);
                group = GetGroup(path, language, "Host", PortalID);
                if (group.Contains(key))
                    return group.GetString(key);
                group = GetGroup(path, language, "", PortalID);
                if (group.Contains(key))
                    return group.GetString(key);

                return null;
            }
            public string GetString(string path, string language, int PortalID, string key)
            {
                if (!alreadySearched.ContainsKey(language))
                {
                    alreadySearched[language] = true;
                    string text = GetStringNoFallBack(path, language, PortalID, key);
                    if (text != null)
                        return text;

                    Locale loc = Localization.GetLocale(language);
                    if (loc != null && !string.IsNullOrEmpty(loc.Fallback))
                        return GetString(path, loc.Fallback, PortalID, key);
                }
                return GetStringNoFallBack(path, Localization.SystemLocale, PortalID, key) ?? key;
            }

            public static ResGroup GetGroup(string path, string language, string mode, int PortalID)
            {
                path = FileMan.MapPath(path);
                path = Localization.GetResourceFileName(path, language, mode, PortalID);
                return (ResGroup)StaticCache2.Get("resx:" + path, delegate()
                {
                    return new ResGroup(path);
                });
            }
        }

        public static string GetString(string path, string language, int PortalID, string key)
        {
            return new RecurciveLanguageSearcher().GetString(path, language, PortalID, key);
        }

        public static string Localize(string key, string path)
        {
            string language = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
            int PortalID = PortalController.GetCurrentPortalSettings().PortalId;
            return GetString(path,language,PortalID, key);
        
        }
    }
}